package com.ocgs.dao.impl;

import com.ocgs.base.core.exception.BaseDAOException;
import com.ocgs.base.util.StringUtil;
import com.ocgs.dao.ProductDAO;
import com.ocgs.mapper.ProductMapper;
import com.ocgs.model.Product;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bryant
 * Date: 10/21/12
 * Time: 3:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProductDAOImpl implements ProductDAO {

    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    
    public int deleteByPrimaryKey(Integer id) throws BaseDAOException{

        try{
            return sqlSessionTemplate.getMapper(ProductMapper.class).deleteByPrimaryKey(id);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    
    public int insert(Product record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(ProductMapper.class).insert(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }

    }
    public int insertSelective(Product record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(ProductMapper.class).insert(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    
    public Product selectByPrimaryKey(Integer id) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(ProductMapper.class).selectByPrimaryKey(id);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    
    public int updateByPrimaryKeySelective(Product record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(ProductMapper.class).updateByPrimaryKeySelective(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    
    public int updateByPrimaryKey(Product record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(ProductMapper.class).updateByPrimaryKey(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    public List<Product> findAllProduct(Product product) throws BaseDAOException{
        try{
            if(product!=null){
                Product search = new Product();
                search.setProductName(StringUtil.escapeSQLString(product.getProductName()));
                search.setProductNo(StringUtil.escapeSQLString(product.getProductNo()));
                search.setBrandId(product.getBrandId());
                search.setCategoryId(product.getCategoryId());
                return sqlSessionTemplate.getMapper(ProductMapper.class).findAllProduct(search);
            } else return null;

        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    public List<Product> getAllProduct() throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(ProductMapper.class).getAllProduct();
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    public List<Product> searchStorage(Product product) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(ProductMapper.class).findAllStorage(product);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }
}
